<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <!--{include file='comm/header.tpl'}-->
    <title>现金卡列表</title>
    <!--{include file='comm/comm_cal.tpl'}-->
    <!--{include file="comm/icheck.tpl"}-->
    <!--{include file='comm/comm_pic.tpl'}-->
</head>
<body>

<div class="container">

    <div class="comm_content">
            <div class="search-box">
                <div style="position:relative">
                    <form class="form-inline" id="search_form" onsubmit="return false;">
                        <table style="width:100%;">
                            <tbody>
                            <tr>
                                <td>
                                    <span>
                                        现金卡名称：<input type="text"  class="form-control input-sm" name="card_name" id="card_name" style="width:120px;"/>
                                    </span>

                                    <span>
                                        现金卡面值：<select class="form-control w150" id="face_value_id" name="face_value_id">
                                            <option value="">请选择</option>
                                            <!--{foreach from=$card_array item=card}-->
                                            <option value="<!--{$card.face_value_id}-->"><!--{$card.face_value}--></option>
                                            <!--{/foreach}-->
                                        </select>
                                    </span>

                                    <span>
                                        现金卡生产状态: <select class="form-control w150" id="card_status" name="card_status">
                                             <option value="">请选择</option>
                                             <option value="1" >待审核</option>
                                             <option value="2" >已审核</option>
                                             <option value="3" >已生产</option>
                                             <option value="4" >已入库</option>
                                         </select>
                                    </span>

                                    <span>
                                        有效开始日期：<input type="text" class="form-control w100" id="startDate" name="startDate" readonly="readonly">
                                    </span>

                                    <span>
                                        有效结束日期：<input type="text" class="form-control w100" id="endDate" name="endDate" readonly="readonly">
                                    </span>

                                    <span>
                                        现金卡类型：<select class="form-control w150" id="card_type" name="card_type">
                                             <option value="">请选择</option>
                                            <!--{foreach from=$cardType_array item=type}-->
                                            <option value="<!--{$type.card_type_id}-->"><!--{$type.card_type_name}--></option>
                                            <!--{/foreach}-->
                                         </select>
                                    </span>

                                    <span><input type="button" value="查询" id="searchBtn" class="btn btn-primary" onclick="cardList.search();" /></span>
                                    <span><input type="button" value="重置" id="clearBtn" class="btn btn-warning" onclick="cardList.clearUp();" /></span>
                                </td>
                            </tr>
                            </tbody>
                        </table>
                    </form>
                </div>
            </div>

            <table id="flex" style="display:none"></table>

            <form name="export_from" id="export_from" method="post" action="/quanliadmin/app/company/card.pscript?operFlg=4">
                <input type="hidden" name="card_id" id="cardId" value="">
                <input type="hidden" name="card_product_number" id="card_number" value="">
            </form>

        </div>

</div>

<!---新增代金卡弹出框start-->
<div id="pop" style="display: none;">

    <div class="panel">

        <div class="panel-body">

            <form id="item_form" onsubmit="return false;" class="form-horizontal">

                <input type="hidden" id="card_id" name="card_id" value="0">

                <div class="form-group">
                    <label class="col-sm-4 control-label" for="save_card_name">
                        <span class="red">*</span>
                        现金卡名称：
                    </label>
                    <div class="col-sm-8">
                        <input type="text" class="form-control w150" placeholder="请输入代金卡名称"  id="save_card_name" name="save_card_name" />
                    </div>
                </div>

                <div class="form-group">
                    <label class="col-sm-4 control-label" for="distribution_id">
                        <span class="red">*</span>
                        现金卡面值：
                    </label>
                    <div class="col-sm-8">
                        <select class="form-control w150" id="face_value_id" name="face_value_id">
                            <option value="">请选择</option>
                            <!--{foreach from=$card_array item=card}-->
                            <option class="face_option_<!--{$card.face_value_id}-->" value="<!--{$card.face_value_id}-->"><!--{$card.face_value}-->—<!--{$card.card_info}--></option>
                            <!--{/foreach}-->
                        </select>
                    </div>
                </div>

                <div class="form-group">
                    <label class="col-sm-4 control-label" for="card_spec_id">
                        <span class="red">*</span>
                        现金卡规格：
                    </label>
                    <div class="col-sm-8">
                        <select class="form-control w150" id="card_spec_id" name="card_spec_id">
                            <option value="">请选择</option>
                            <!--{foreach from=$card_spec_array item=card_spec}-->
                            <option class="card_spec_<!--{$card_spec.itemID}-->" value="<!--{$card_spec.itemID}-->"><!--{$card_spec.itemName}--></option>
                            <!--{/foreach}-->
                        </select>
                    </div>
                </div>

                <div class="form-group">
                    <label class="col-sm-4 control-label">
                        <span class="red">*</span>
                        城市：
                    </label>
                    <div class="col-sm-8">
                        <!--{foreach from=$city_array item=city}-->
                        <div class="fl" style="margin: 0px 10px 10px 0px;">
                            <input type="checkbox" class="square-radio" id="city-checkbox-<!--{$city.id}-->" name="cityID" value="<!--{$city.id}-->">
                            <label for="city-checkbox-<!--{$city.id}-->"><!--{$city.name}--></label>
                        </div>
                        <!--{/foreach}-->
                    </div>
                </div>

                <div class="form-group">
                    <label class="col-sm-4 control-label" for="save_card_type">
                        <span class="red">*</span>
                        现金卡类型：
                    </label>
                    <div class="col-sm-8">
                        <select class="form-control w150" id="save_card_type" name="save_card_type">
                            <option value="">请选择</option>
                            <!--{foreach from=$cardType_array item=type}-->
                            <option value="<!--{$type.card_type_id}-->"><!--{$type.card_type_name}--></option>
                            <!--{/foreach}-->
                        </select>
                    </div>
                </div>

                <div class="form-group">
                    <label class="col-sm-4 control-label" for="card_product_number">
                        <span class="red">*</span>
                        现金卡生产数量：
                    </label>
                    <div class="col-sm-8">
                        <input type="text" class="form-control w150" placeholder="请输入生产数量"  id="card_product_number" name="card_product_number" />
                    </div>
                </div>

                <div class="form-group">
                    <label class="col-sm-4 control-label" for="can_use_number">
                        <span class="red">*</span>
                        可使用次数：
                    </label>
                    <div class="col-sm-8">
                        <input type="text" class="form-control w150" placeholder="请输入可使用次数"  id="can_use_number" name="can_use_number" value="100" />
                    </div>
                </div>

                <div class="form-group">
                    <div>
                        <label class="col-sm-4 control-label" for="save_startDate">
                            <span class="red">*</span>
                            有效期开始日期：
                        </label>
                        <div class="col-sm-8">
                            <input type="text" class="form-control w150" id="save_startDate" name="save_startDate" readonly="readonly">
                        </div>
                    </div>
                </div>

                <div class="form-group">
                    <div>
                        <label class="col-sm-4 control-label" for="save_endDate">
                            <span class="red">*</span>
                            有效期结束日期：
                        </label>
                        <div class="col-sm-8">
                            <input type="text" class="form-control w150" id="save_endDate" name="save_endDate" readonly="readonly">
                        </div>
                    </div>
                </div>

                <div class="form-group">
                    <label class="col-sm-4 control-label">
                        <span class="red">*</span>
                        商品筛选器：
                    </label>
                    <div class="col-sm-8">
                        <select class="form-control w150 fl" name="goods_selector_id" id="goods_selector_id" >
                            <option value="">请选择</option>
                        </select>
                    </div>
                </div>

                <div class="form-group" >
                    <label class="col-sm-4 control-label" for="remark">
                        备注：
                    </label>
                    <div class="col-sm-8">

                        <textarea id="remark" class="form-control w300" rows="3" name="remark"  placeholder="请输入备注"></textarea>

                    </div>
                </div>

            </form>
        </div>
    </div>
</div>

<script>

goods_selector = <!--{$goods_selector|default:''}-->;

$("#flex").flexigrid({
    url:"/invokingAjax-backend/company/card.ajax?operFlg=1",
    dataType: "json",
    colModel : [
        {display: "操作", width : 150, align:"center"},
        {display: "生产单号", name : "card_id", sortable : true,width : 80,align:"left"},
        {display: "现金卡名称", width : 150,align:"left"},
        {display: "现金卡面值", width : 100,align:"left"},
        {display: "现金卡数量", width : 100,align:"left"},
        {display: "使用次数", width : 100,align:"left"},
        {display: "生产状态", width : 100,align:"center"},
        {display: "入库状态", width : 100,align:"center"},
        {display: "有效开始日期", width : 140,align:"center"},
        {display: "有效结束日期", width : 140,align:"center"},
        {display: "现金卡类型", width : 150,align:"left"},
        {display: "现金卡规格", width : 80,align:"center"},
        {display: "使用城市", width : 120,align:"left"},
        {display: "商品筛选器", width : 120,align:"left"},
        {display: "备注", width : 300,align:"left"}
    ],
    buttons : [
        {name: "添加", bclass: "add", onpress : "cardList.edit.open"}
    ],
    sortname: "card_id",
    sortorder: "desc",
    usepager: true,
    rp: 10,
    useRp: true,
    showTableToggleBtn: true,
    onSubmit: function() {
        var dt = $('#search_form').serializeArray();
        $("#flex").flexOptions({params: dt});
        return true;
    }
});
//日期控件JS
$('#startDate').datepicker();
$('#endDate').datepicker();

var cardList ={

    card_id:'',//现金卡生产单ID

    //头部 按条件查询
    search: function() {
        var dt = $('#search_form').serializeArray();
        $("#flex").flexOptions({params: dt}).flexReload();
    },


    //重置
    clearUp:function() {
        $("#card_name").val('');
        $("#face_value_id").val('');
        $("#card_status").val('');
        $("#startDate").val('');
        $("#endDate").val('');
        $("#card_type").val('');
        this.search();

    },

    //添加或修改现金卡
    edit: {

        open: function( card_id ) {


            if (!card_id) {
                card_id = 0;

            }else{
                cardList.card_id = card_id;
            }


            E.popup.open({
                id: 'pop',
                title: card_id == 0 ? '添加现金卡' : '修改现金卡',
                css: 'width: 600px;',
                btnName: '保存',
                btnCall: 'cardList.edit.check();'
            });

            $('.square-radio').iCheck({
                checkboxClass: 'icheckbox_square-blue',
                radioClass: 'iradio_square-blue',
                increaseArea: '20%' // optional
            });


            $("#save_card_type").change(function(){
                var card_type = $(this).val();
                if(card_type==1)
                {
                    $("#goods_selector_id").val(209);
                    $("#goods_selector_id").attr('disabled','disabled');
                }
                else
                {
                    $("#goods_selector_id").val("");
                    $("#goods_selector_id").removeAttr("disabled");
                }

            });

            //日期控件JS
            $('#save_startDate').datepicker();
            $('#save_endDate').datepicker();

            $('.square-checkbox').iCheck({
                checkboxClass: 'icheckbox_square-blue',
                radioClass: 'iradio_square-blue',
                increaseArea: '20%' // optional
            });



            if (card_id > 0) {

                E.ajax_get({
                    url: '/invokingAjax-backend/company/card.ajax?operFlg=2',
                    data: {
                        card_id: card_id
                    },
                    call: function( o ) {

                        if (o.code == 200) {
                            $('#card_id').val(o.data.card_id);
                            $('#save_card_name').val(o.data.card_name);
                            $('.face_option_' + o.data.face_value_id ).attr("selected", true);
                            $('#card_product_number').val(o.data.card_product_number);
                            $('#can_use_number').val(o.data.can_use_number);
                            $('#save_card_status').val(o.data.card_status);
                            $('#save_startDate').val(o.data.startDate);
                            $('#save_endDate').val(o.data.endDate);
                            $('#save_card_type').val(o.data.card_type);
                            $('#remark').val(o.data.remark);
                            $('#card_spec_id' ).val(o.data.card_spec_id);

                            $.each(o.data.city,function(k,v){
                                $('#city-checkbox-' + v.cityID ).iCheck('check');
                            });

                            if(o.data.card_type==1)
                            {
                                $("#goods_selector_id").attr('disabled','disabled');
                            }
                            else
                            {
                                $("#goods_selector_id").removeAttr('disabled');
                            }


                            //商品筛选器
                            var goods_selector_html = '<option value="">请选择</option>';
                            $.each(goods_selector, function(key, value){
                                goods_selector_html += '<option value="' + value.selectID + '"';
                                if(o.data.goods_selector_id == value.selectID)
                                {
                                    goods_selector_html += 'selected';
                                }
                                goods_selector_html += '>' + value.selectName + '</option>';
                            });
                            $("#goods_selector_id").html( goods_selector_html );


                        }

                    }
                });

            } else {

                //商品筛选器
                var goods_selector_html = '<option value="">请选择</option>';
                $.each(goods_selector, function(key, value){
                    goods_selector_html += '<option value="' + value.selectID + '">' + value.selectName + '</option>';
                });
                $("#goods_selector_id").html( goods_selector_html );

            }

        },

        check: function() {

            this.dt = E.getFormValues('item_form');

            this.dt.card_spec = this.dt.card_spec_id != "" ? $("#card_spec_id option:selected").text() : '';

            var error_msg = '';

            if (E.isEmpty(this.dt.save_card_name))
                error_msg += '请填写现金卡名称<br/>';

            if (this.dt.face_value_id == '')
                error_msg += '请选择现金卡面值<br/>';

            if (E.isEmpty(this.dt.card_product_number)){
                error_msg += '请填写现金卡生产数量<br/>';

            } else {

                if( !E.isInt(this.dt.card_product_number) )
                    error_msg += "现金卡数量只能为正整数<br />";

            }

            if (this.dt.can_use_number == ''){
                error_msg += '请填写使用次数<br/>';

            } else if(this.dt.can_use_number < 0 || this.dt.can_use_number == 0){
                error_msg += "使用次数不能小于或等于零<br />";

            } else {

                if( !E.isInt(this.dt.can_use_number) )
                    error_msg += "使用次数只能为正整数<br />";

            }

            if (this.dt.save_card_status == '')
                error_msg += '请选择现金卡生产状态<br/>';


            if (!E.isDate(this.dt.save_startDate))
                error_msg += "有效期开始时间格式不对<br />";

            if (!E.isDate(this.dt.save_endDate))
                error_msg += "有效期结束时间格式不对<br />";

            if(this.dt.save_startDate > this.dt.save_endDate)
                error_msg += "有效期开始时间不能大于结束时间<br />";

            if (this.dt.save_card_type == '')
                error_msg += '请填写卡系统卡类型<br/>';

            if (this.dt.goods_selector_id == '')
                error_msg += '请填写商品筛选器<br/>';

            if (this.dt.cityID == '')
                error_msg += '请选择城市<br/>';

            if(this.dt.card_spec_id == '')
                error_msg += '请选择现金卡规格<br/>';

            if (error_msg != '') {
                E.alert(error_msg);

            } else {

                if( cardList.card_id ){
                    E.confirm('您确认修改现金卡吗？', 'cardList.edit.execute');
                }else{
                    E.confirm('您确认添加现金卡吗？', 'cardList.edit.execute');
                }



            }

        },

        execute: function() {

            E.loadding.open('正在保存，请稍候...');

            E.ajax_post({
                url: '/invokingAjax-backend/company/card.ajax?operFlg=100&act=edit',
                data: this.dt,
                call: 'cardList.edit.result'
            });

        },

        result: function( o ) {

            E.loadding.close();

            if (o.code == 200) {

                if (this.dt.card_id > 0) {
                    E.popup.close();
                } else {
                    document.getElementById('item_form').reset();
                }

                E.alert(o.message, 2, 'cardList.load');
            } else {
                E.alert(o.message);
            }

        }

    },

    del: {

        check: function( card_id ) {
            cardList.card_id = card_id;
            E.confirm('您确认要删除选中的现金卡吗？', 'cardList.del.execute');

        },

        execute: function() {
            E.loadding.open('正在删除，请稍候...');
            E.ajax_post({
                url: '/invokingAjax-backend/company/card.ajax?operFlg=100&act=del',
                data: {
                    card_id:cardList.card_id
                },
                call: 'cardList.del.result'
            });
        },

        result: function( o ) {

            E.loadding.close();
            if (o.code == 200) {
                E.alert('删除成功', 2, 'cardList.load');
            } else {
                E.alert(o.message);
            }

        }

    },

    //生产现金卡
    add: function( card_id ) {

        this.card_id = card_id;

            E.confirm('您确认生产，生产单号为'+ card_id +'的现金卡吗？', 'cardList.card');


    },

    card: function() {

        E.loadding.open('正在生产，请稍候...');

        E.ajax_post({
            url: '/invokingAjax-backend/company/card.ajax?operFlg=7',
            data: {card_id:this.card_id},
            call: 'cardList.results'
        });

    },

    results: function( o ) {

        E.loadding.close();

        if (o.code == 200) {

            if (this.card_id > 0) {
                E.popup.close();
            } else {
                document.getElementById('item_form').reset();
            }

            E.alert(o.message, 2, 'cardList.load');
        } else {
            E.alert(o.message);
        }

    },

    //编号查询
    inquiredNumber: function(card_id){
        self.location = '/quanliadmin/app/company/card.pscript?operFlg=5&card_id='+card_id;
    },

    //审核确认
    audit:function(card_id){
        this.card_id = card_id;
        E.confirm('您确认审核该现金卡吗?', "cardList.auditExe");
    },

    //执行审核
    auditExe: function () {
        var args = new Array();
        args.url = "/invokingAjax-backend/company/card.ajax?operFlg=3"
        args.data = {card_id:cardList.card_id};
        args.call = 'cardList.callback';
        E.ajax_post(args);
        E.popup.close();
    },

    //回调函数
    callback: function( obj ) {

        if( obj.code == 200 ){
            E.alert( obj.message , 2 , 'cardList.load');
        }else{
            E.alert(obj.message);
        }

    },

    //导出现金卡信息
    exportCard:function( card_id , card_product_number ){
        $("#cardId").val( card_id );
        $("#card_number").val( card_product_number );
        var cardSum = card_product_number;

        if( cardSum <= 5000 ){
            cardList.export();
            return false;
        }

        var page = Math.ceil( cardSum/5000 );

        this.totalPage = page;

        var html = "<table>";
        html += "<tr>";
        html += "<td width=\"100\" style='text-align: center;'><b>当前数据已超过5000条，请选择区间导出</b></td>";
        html += "</tr>";
        html += "<tr>";
        html += "<td width=\"100\" style='text-align: center;'>5000条为一个区间，当前数据共 <b style='font-size: 14px;'>"+page+"</b> 页</td>";
        html += "</tr>";
        html += "<tr>";
        html += "<td>导出第<input type='text' id='page' value='1' >页数据</td>";
        html += "</tr>";
        html += "</tbody>";
        html += "</table>";

        E.popup.open({
            title: "选择现金卡导出区间",
            content: html,
            btnName: "导出",
            css:"width:300px;",
            btnCall: "cardList.choseExportRegion();"
        });

    },

    //选择打印区间
    choseExportRegion:function(){

        var page = $.trim( $("#page").val() );

        if( page == '' ){
            E.alert('请填写导出页数');
            return false;
        }

        if( !E.isInt( page ) || page <= 0 ){
            E.alert('请填写正确的页数');
            return false;
        }

        if( page > this.totalPage ){
            E.alert('导出页数不能大于总页数');
            return false;
        }

        document.getElementById('export_from').action = '/quanliadmin/app/company/card.pscript?operFlg=4&page='+page;
        document.getElementById('export_from').submit();

    },

    export: function() {
        document.getElementById('export_from').action = '/quanliadmin/app/company/card.pscript?operFlg=4';
        document.getElementById('export_from').submit();
    },

    //现金卡入库
    putinStore:function(card_id){
        E.open("/quanliadmin/app/company/card.pscript?operFlg=3&card_id="+card_id, "" , {height: 500,width:880, top: 50, left: 250});
    },

    //现金卡调拨
    allot:function(card_id){
        E.open("/quanliadmin/app/company/card.pscript?operFlg=6&card_id="+card_id, "" , {height: 500,width:880, top: 50, left: 250});
    },


    //重新加载表格
    load: function() {
        $("#flex").flexReload();
    }

};

</script>

</body>
</html>